package cc.chenwh.sequence.client.sequenceclient;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;

public class M2 {
    private final ReentrantLock lock = new ReentrantLock(true);
    private final AtomicLong sequence = new AtomicLong(0);

    private static long lockTime = 0;
    private static long nolockTime = 0;

    public static void main(String[] args) {
        M2 m = new M2();
        for (int i = 0; i < 100000 ; i++) {
            m.fNoLock();
            m.fLock();
        }
        System.out.println(lockTime);
        System.out.println(nolockTime);
    }

    public void fNoLock(){
        long sttime = System.nanoTime();
        sequence.incrementAndGet();
        long itv = System.nanoTime() - sttime;
        nolockTime+=itv;
    }

    public void fLock(){
        long sttime = System.nanoTime();
        lock.lock();
        try{
            sequence.incrementAndGet();
        }finally {
            lock.unlock();
        }
        long itv = System.nanoTime() - sttime;
        lockTime += itv;
    }
}
